﻿import com.crystalcentral.*;
import com.crystalcentral.model.*;
import com.crystalcentral.view.*;
import com.crystalcentral.util.*;

import mx.utils.Delegate;

class com.crystalcentral.DiyInfoLoader
{
	static private var diyInfoLoader:DiyInfoLoader;
	
	private var diyTool:DiyTool;
	private var diyInfoXML:XML;
	private var diyInfo:DiyInfo;
	private var onLoadCallback:Function;

	private function DiyInfoLoader()
	{
		this.diyTool = DiyTool.getInstance();
		
		this.diyInfoXML = new XML();
		this.diyInfoXML.onLoad = Delegate.create(this, onLoadDiyInfo);
	}
	
	public static function getInstance() {
		if (diyInfoLoader == null || diyInfoLoader == undefined) {
			diyInfoLoader = new DiyInfoLoader();
		}
		return diyInfoLoader;
	}

	function loadDiyInfo(src:String, callback:Function)
	{
		this.onLoadCallback = callback;
		diyInfoXML.ignoreWhite = true;
		diyInfoXML.load(src);
	}
	
	function onLoadDiyInfo(success:Boolean):Void
	{
		if (success) {
			diyInfo = new DiyInfo(diyInfoXML);
			diyTool.diyInfo = diyInfo;
			onLoadCallback.apply(diyTool);
		}
		else {
			trace("Failed to load DiyInfo");
		}
		diyTool.stopLoading();
	}
}